package com.baitiaojun.common.utils.exception;


import com.baitiaojun.common.enums.Enums;
import com.baitiaojun.common.enums.impl.CommonStatusEnums;
import com.baitiaojun.common.exception.GlobalException;
import com.baitiaojun.common.exception.ServiceException;
import com.baitiaojun.common.utils.string.FormatterUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * 异常信息工具,打印完整异常信息的同时不阻断线程执行
 */
public class ExceptionUtils {

//    public static GlobalException exp(Enums enums) {
//        return exp(enums.getCode(), enums.getName());
//    }
//
//    public static GlobalException exp(Enums enums, Object... args) {
//        return exp(enums.getCode(), enums.getName(), args);
//    }

    /**
     * 按照异常模版格式化异常信息
     * @param code
     * @param message
     * @param args
     * @return
     */
//    public static GlobalException exp(Integer code, String message, Object... args) {
//        if (code == null) {
//            code = CommonStatusEnums.FAIL.getCode();
//        }
//        if (StringUtils.isEmpty(message)) {
//            message = CommonStatusEnums.FAIL.getName();
//        }
//        return new ServiceException(code, FormatterUtils.format(message, args));
//    }

    /**
     * 获得异常信息
     * @param throwable
     * @return
     */
    public static String getExpMsg(Throwable throwable) {
        StringWriter stringWriter = new StringWriter();
        throwable.printStackTrace(new PrintWriter(stringWriter, true));
        return stringWriter.toString();
    }

//    public static void main(String[] args) {
//        Exception exception = new Exception("测试");
//        System.out.println(getExpMsg(exception));
//        System.out.println("继续执行");

    //    Integer code = BusinessStatusEnums.FAIL.getCode();
//        ServiceException exception = GlobalExceptionHelper.builder().exception(new ErrorCode(code, "异常返回"));
//        ServiceException exception = GlobalExceptionHelper.builder().exception("异常返回{}", "data");
//        ServiceException exception = GlobalExceptionHelper.builder().exception(code, "异常返回");
//        System.out.println(exception.getCode() + exception.getMessage());
//    }
}
